package bb.lanxing.device.sync;

import android.util.Log;

import java.util.LinkedHashMap;
import java.util.Timer;
import java.util.TimerTask;

import bb.lanxing.model.database.Device;


public abstract class XossDeviceAutoSyncTimerTask implements XossDeviceAutoSyncTask {
    private LinkedHashMap<String, Timer> timers = new LinkedHashMap<>();

    public abstract long getTimePeriod();

    public abstract boolean isSupport(String str);

    public final LinkedHashMap<String, Timer> getTimers() {
        return this.timers;
    }

    public final void setTimers(LinkedHashMap<String, Timer> linkedHashMap) {
        this.timers = linkedHashMap;
    }

    public final void startTimerTask(final String address, final String name, final int i) {
        if (isSupport(address) && Device.getByAddress(address) != null) {
            stopTimerTask(address, name);
            Timer timer = this.timers.get(address);
            if (timer == null) {
                timer = new Timer();
                this.timers.put(address, timer);
            }
            timer.schedule(new TimerTask() {
                @Override
                public void run() {
//                    this.onSync(address, name, i);
                }
            }, getTimePeriod(), getTimePeriod());
            return;
        }
        stopTimerTask(address, name);
    }

    public final void stopTimerTask(String address, String name) {
        Timer timer = this.timers.get(address);
        if (timer != null) {
            timer.cancel();
        }
        if (timer != null) {
            timer.purge();
        }
        this.timers.remove(address);
    }
}
